//
//  UIAlertController+HHPExtension.m
//  HHPLiveVideo
//
//  Created by 韩红坡 on 2017/4/13.
//  Copyright © 2017年 韩红坡. All rights reserved.
//

#import "UIAlertController+HHPExtension.h"
#import "NSObject+CurrentVC.h"

@implementation UIAlertController (HHPExtension)

+ (id)hhp_showAlertControllerWithPreferredStyle:(UIAlertControllerStyle)preferredStyle
                        WithTitle:(NSString *)title
                          message:(NSString *)message
                cancelButtonTitle:(NSString *)cancelButtonTitle
                otherButtonTitles:(NSArray *)otherButtonTitles
                          handler:(void (^)(NSInteger buttonIndex))block{
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:preferredStyle];
    
    //define cancelAction
    UIAlertAction *cancelAction = nil;
    if (cancelButtonTitle.length) {
        cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            if (block) {
                block(0);
            }
        }];
    }
    if (cancelAction) {
        [alertController addAction:cancelAction];
    }
    
    //define other actions
    [otherButtonTitles enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        NSString *curTitle = nil;
        if ([obj isKindOfClass:[NSString class]]) {
            curTitle = (NSString *)obj;
        }
        
        if (curTitle) {
            UIAlertAction *subAction = [UIAlertAction actionWithTitle:curTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                if (block) {
                    block(idx + 1);
                }
            }];
            [alertController addAction:subAction];
        }
        
    }];
    
    [self.getCurrentVC presentViewController:alertController animated:YES completion:nil];
    
    return alertController;
}

@end
